package com.jcgyl.export.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jcgyl.export.entity.Department;
import com.jcgyl.export.entity.DepartmentQueryModel;
import com.jcgyl.export.service.DepartmentService;
import com.jcgyl.export.util.entity.Result;

@RestController
@RequestMapping("/department")
public class DepartmentController {
	@Autowired
	private DepartmentService departmentService;

	// 获取所有的部门信息
	@PostMapping("/findAllDepartment")
	public Map<String, Object> doFindAllDepartment(@RequestBody DepartmentQueryModel departmentFilter) {
		String departmentName = departmentFilter.getFilter().getDepartmentName();
		String parentName = departmentFilter.getFilter().getParentName();
		int level;
		if( departmentFilter.getFilter().getLevel()!=null){
		      level=departmentFilter.getFilter().getLevel();
		}else{
			 level=0;
		};
		System.out.println(
				"接受客户前台客户信息" + "departmentName:" + departmentName + "parentName:" + parentName + "level:" + level);
		int pageIndex = departmentFilter.getPageIndex();
		int pageSize = departmentFilter.getPageSize();		
		System.out.println("pageIndex:" + pageIndex + "pageSize:" + pageSize);
		Map<String, Object> map = departmentService.findAllDepartment(departmentName, parentName, level, pageSize,
				pageIndex);
		return map;
	}
	
	@RequestMapping("/findDepartmentName")
	public List<Department> doFIndDepartmentName() {
		return departmentService.findDepartmentName();
	}
	

	@PostMapping("/findZtreeObject")
	public List<Department> doFindZtreeObject() {
		return departmentService.findZtreeObject();
	}
	// 添加修改部门信息
	@RequestMapping("/addOrUpdateDepartment")
	public Result doAndOrUpdateDepartment(@RequestBody Department department) {
		if (department.getId() == null) {
			return departmentService.addDepartment(department);
		} else {
			return departmentService.updateDepartment(department);
		}
	}

	// 删除部门信息
	@RequestMapping("/deleteDepartment")
	public Result DeleteDepartment(@RequestBody Department department) {
		int departmenId=department.getId();
		System.out.println(departmenId);
		if(department.getId()<=0){
			return Result.build(1, "请重新选择");
		}
		return departmentService.deleteDepartment(departmenId);
	}
}
